//
//  BBLeafNodeView.m
//  xNose
//
//  Created by ben smith on 12/09/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBLeafNodeView.h"
#import "BBNode.h"
#import "BBDefaultController.h"
#import "BBImageButtonFactory.h"
#import "BBNodeViewFactory.h"
#import "BBConnectingButton.h"
#import "BBNodeDrawingFactory.h"

@implementation BBLeafNodeView
- (id)initWithFrame:(NSRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		// add the disclosure button
	//	[self setWantsLayer:YES];
		//[self scaleUnitSquareToSize:NSMakeSize(0.5, 0.5)];
		float buttonSize = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBChartButtonSize"];
		float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
		
		NSRect contentRect = [backgroundRect bounds];
		
		float yOffset = NSMidY(contentRect) - (buttonSize/2.0) + 1;
		
		NSRect infoRect = NSMakeRect(NSMaxX(contentRect) - buttonSize - borderHeight - 5, yOffset, buttonSize, buttonSize);
		
		NSButton * chartButton = [BBImageButtonFactory bigChartButtonInRect:infoRect];
		
		[chartButton setTarget:self];
		[chartButton setAction:@selector(chartClick)];
	//	[chartButton setWantsLayer:YES];
		
		[self addSubview:chartButton];
		
		NSRect connectRect = [BBNodeViewFactory connectionButtonRectForView:self];
		BBConnectingButton * conButton = [[BBConnectingButton alloc] initWithFrame:connectRect];
//		[conButton setWantsLayer:YES];
		[conButton setDelegate:self];
		[self addSubview:conButton];
	}
	return self;
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
	NSGraphicsContext *nsGraphicsContext;
	nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx
																																 flipped:NO];
	[NSGraphicsContext saveGraphicsState];	
	[NSGraphicsContext setCurrentContext:nsGraphicsContext];
//	[BBNodeDrawingFactory drawLeafNode:self.node];
	[self drawNode];
	[NSGraphicsContext restoreGraphicsState];
}


-(void)chartClick
{
	[[NSNotificationCenter defaultCenter] postNotificationName:@"BBOpenControllerNotification" object:self.node];
}


-(void)drawNode
{
	[self.backgroundGradient drawInBezierPath:backgroundRect angle:90.0];		
	[[NSColor whiteColor] set];
	[backgroundRect stroke];
	
//	[[NSColor redColor] set];
//	[NSBezierPath strokeRect:[self bounds]];
}


-(void)setupBackgroundRect
{
	float leftInset = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBLeftInsetWidth"];
	float leafHeightInset = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBLeafHeightInset"];
	float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
	
	NSRect contentRect = NSMakeRect(NSMinX(self.bounds) + leftInset, NSMinY(self.bounds) + (leafHeightInset), NSWidth(self.bounds) - leftInset - (borderHeight * 2.0), NSHeight(self.bounds) - (leafHeightInset) - 1);
	
	NSRect containRect = contentRect;
	containRect.origin.x = floor(containRect.origin.x) + 0.5;
	containRect.origin.y = floor(containRect.origin.y) + 0.5;
	containRect.size.width = floor(containRect.size.width);
	containRect.size.height = floor(containRect.size.height);
	
	self.backgroundRect = [NSBezierPath bezierPathWithRoundedRect:containRect xRadius:10 yRadius:10];
	[backgroundRect setLineWidth:2.0];
}


-(NSRect)textRect
{
	NSRect textRect = [backgroundRect bounds];

	float textInset = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBTextInsetWidth"];
	float buttonSize = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBInfoButtonSize"];
	float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
	
	float fieldHeight = [BBDefaultController fontHeight:[self font]];
	
	textRect.size.height = fieldHeight;
	textRect.origin.y = NSMidY([backgroundRect bounds]) - (fieldHeight/2.0);
	textRect.size.width = NSWidth([backgroundRect bounds]) - buttonSize - (borderHeight * 2.0) - textInset - (borderHeight * 2.0);
	textRect.origin.x += textInset + borderHeight + 8;
	
	return textRect;	
}

-(NSFont*)font
{
	return [BBDefaultController regularSmallFont];
}

- (void)setupTextAttributes
{
	[super setupTextAttributes];
	[titleField setAlignment:NSLeftTextAlignment];
}	

- (void)setupBackgroundGradient
{
	// create a basic gradient for the background of the view
	CGFloat red1   =   240.0 / 255.0;
	CGFloat green1 =   240.0 / 255.0;
	CGFloat blue1  =  240.0 / 255.0;
	
	CGFloat red2    =  150.0 / 255.0;
	CGFloat green2  =  150.0 / 255.0;
	CGFloat blue2   =  150.0 / 255.0;
	
	NSColor* gradientTop    = [NSColor colorWithCalibratedRed:red1 green:green1 blue:blue1 alpha:1.0];    
	NSColor* gradientBottom = [NSColor colorWithCalibratedRed:red2 green:green2 blue:blue2 alpha:1.0];
	
	NSGradient* gradient;
	gradient = [[NSGradient alloc] initWithStartingColor:gradientBottom endingColor:gradientTop];
	
	self.backgroundGradient = gradient;
	[gradient release];
}
@end
